#include "CGameStatesStack.h"

CGameStatesStack::CGameStatesStack()
{
	m_pStatesStack = new StackState();
	m_pStatesStack->last = m_pStatesStack;
	m_pStatesStack->prev = 0;
	m_pStatesStack->state = EGS_INIT;
}

void CGameStatesStack::push( EGameStates newState )
{
	StackState *newStackState = new StackState();

	newStackState->last = newStackState;
	newStackState->prev = m_pStatesStack;
	newStackState->state = newState;
	
	m_pStatesStack = newStackState;
	
	m_stackSize++;
}

EGameStates CGameStatesStack::pop()
{
	if(m_stackSize)
	{
		StackState *tmpStackState = new StackState();
		tmpStackState = m_pStatesStack;
		EGameStates tmpState = tmpStackState->state;

		m_pStatesStack = m_pStatesStack->prev;

		delete tmpStackState;

		m_stackSize--;

		return tmpState;
	}
	else
		return EGS_INIT;
}

EGameStates CGameStatesStack::getCurrentState() const
{
	return m_pStatesStack->state;
}

bool CGameStatesStack::isEmpty() const
{
	return !m_stackSize;
}

CGameStatesStack::~CGameStatesStack()
{
	for (unsigned int i = m_stackSize; i > 0; i--)
		pop();
}
